﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Apache.NMS;
using Apache.NMS.Util;


namespace GrootHandelWebService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "WCFGrootHandel" in code, svc and config file together.
    public class WCFGrootHandel : IWCFGrootHandel
    {

        public bool plaatsBestelling(Bestelling bestelling)
        {

            System.Diagnostics.Debug.Write("Nieuwe bestelling");

            if (bestelling.Naam == "")
            {
                return false;
            }
            else
            {
                Uri connecturi = new Uri("tcp://localhost:61616");

                Console.WriteLine("About to connect to " + connecturi);

                // NOTE: ensure the nmsprovider-activemq.config file exists in the executable folder.
                IConnectionFactory factory = new NMSConnectionFactory(connecturi);

                using (IConnection connection = factory.CreateConnection())
                using (ISession session = connection.CreateSession())
                {

                    IDestination destination = SessionUtil.GetDestination(session, "queue://bevestiging");
                    Console.WriteLine("Using destination: " + destination);

                    // Create a consumer and producer
                    using (IMessageConsumer consumer = session.CreateConsumer(destination))
                    using (IMessageProducer producer = session.CreateProducer(destination))
                    {
                        // Start the connection so that messages will be processed.
                        connection.Start();
                        // producer.Persistent = true;
                        //producer.

                        // Send a message
                        ITextMessage request = session.CreateTextMessage("Bestelling is geplaatst bij Groothandel A");
                        request.NMSCorrelationID = "A"; //Groothandel A
                        request.Properties["Groothandel"] = "a";
                        request.Properties["BestellingsID"] = bestelling.Id;
                        request.Properties["BestellingsProductNaam"] = bestelling.Naam;
                        request.Properties["BestellingsAantal"] = bestelling.Aantal;
                        request.Properties["BestellingsProductsoort"] = bestelling.ProductSoort;
                        request.Properties["Bevestigd"] = "Ja";
                       // request.Properties["myHeader"] = "Cheddar";

                        producer.Send(request);


                    }
                    return true;
                }
            }
        }
    }
}

